﻿namespace Web.Control.WWW.Contacts
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections;

    using Lx.Web.Common.ControlsUI;
    using BLF.WWW_WWW.Contact;
    using System.Collections.Generic;
    using System.Reflection;
    /// <summary>
    /// SearchPartner 的摘要说明
    /// </summary>
    public class Search : BaseControl
    {
        /// <summary>
        /// 学习语言
        /// </summary>
        protected string learnVal = "";
        /// <summary>
        /// 母语
        /// </summary>
        protected string speakVal = "";
        /// <summary>
        /// 当前会员信息
        /// </summary>
        protected Model.WWW_User.User.UserSummaryInfoModel memberInfo = new Model.WWW_User.User.UserSummaryInfoModel();
        private int pageIndex = 1;
        private int pageSize = 20;
        protected Repeater uList;
        protected Literal litScript;
        /// <summary>
        /// 学习语言
        /// </summary>
        protected string learnValc = "";
        /// <summary>
        /// 使用语言
        /// </summary>
        protected string speakValc = "";
        protected string nationValc = "";
        /// <summary>
        /// 最大年龄
        /// </summary>
        protected string maxAgeValc = "200";
        /// <summary>
        /// 最小年龄
        /// </summary>
        protected string minAgeValc = "0";
        protected string genderValc = "";        
        protected int isAvatarValc = 0;
        protected int isChatNowValc = 0;
        private string memberno = "";
        /// <summary>
        /// 查询值数量
        /// </summary>
        protected int SearchCount = 0;
        
        protected override void OnLoad(EventArgs e)
        {
            //List<Model.WWW_User.User.UserSummaryInfoModel> userList;
            ArrayList arrList = new ArrayList();
            memberno= HttpContext.Current.User.Identity.Name;
            
            if (!String.IsNullOrEmpty(Request["learn"]) || !String.IsNullOrEmpty(Request["speak"]))
            {
                learnVal = Request["learn"] == null ? "" : Request["learn"];
                speakVal = Request["speak"] == null ? "" : Request["speak"];
                learnValc = learnVal;
                speakValc = speakVal;
                isChatNowValc = 0;
                arrList = Contacts.GetMembers(learnVal, speakVal, "", "200", "0", "", false, false, pageIndex, pageSize, memberno);
            }//string learnL, string speakL, string nation, int maxAge, int minAge, string gender, bool isAvatar, bool isChatNow, int pageIndex, int pageSize, string memberno
            else
            {
                if (memberno != "")
                {//读取cookie用户设置的值
                    if (Request.Cookies["italkiUserSearchKey"] != null && Request.Cookies["italkiUserSearchKey"].Value != "" && !String.IsNullOrEmpty(Request.Cookies["italkiUserSearchKey"].Value))
                    {
                        string userSearCookStr = Request.Cookies["italkiUserSearchKey"].Value;
                        string cMemberno = GetUserSearCookieFieldVal(userSearCookStr, "memberno");
                        if (memberno == cMemberno)
                        {
                            learnValc = GetUserSearCookieFieldVal(userSearCookStr, "learn");
                            speakValc = GetUserSearCookieFieldVal(userSearCookStr, "speak");
                            nationValc = GetUserSearCookieFieldVal(userSearCookStr, "nation");
                            string max = GetUserSearCookieFieldVal(userSearCookStr, "maxAge");
                            string min = GetUserSearCookieFieldVal(userSearCookStr, "minAge");
                            maxAgeValc = (max != "" ? max : "200");
                            minAgeValc = (min != "" ? min : "0");
                            genderValc = GetUserSearCookieFieldVal(userSearCookStr, "gender");
                            isAvatarValc = (GetUserSearCookieFieldVal(userSearCookStr, "isAvatar") == "1" ? 1 : 0);
                            isChatNowValc = (GetUserSearCookieFieldVal(userSearCookStr, "isChatNow") == "1" ? 1 : 0);
                        }
                    }
                }
                arrList = Contacts.GetMembers(learnValc, speakValc, nationValc, maxAgeValc, minAgeValc, genderValc, isAvatarValc == 1 ? true : false, isChatNowValc == 1 ? true : false, pageIndex, pageSize, memberno);
            }
            if (arrList.Count > 1)
            {
                SearchCount = (int)arrList[1];
                if (memberno != "")
                {
                    memberInfo = arrList[2] as Model.WWW_User.User.UserSummaryInfoModel;
                }

                if (uList != null)
                {
                    uList.DataSource = arrList[0];
                    uList.DataBind();
                }
                if (litScript != null)
                {
                    litScript.Text = "<script>var speakL='" + speakVal;
                    litScript.Text += "';var Memberno='" + memberno;
                    litScript.Text += "';var learnL='" + learnVal;
                    litScript.Text += "';var userPage=" + (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(new BLF.PageableObject(SearchCount, pageIndex, pageSize)) + ";</script>";
                }
            }
            else {
                Response.End();
                return;
            }
            base.OnLoad(e);
        }

        /// <summary>
        /// 根据存储的cookie字符串得到具体的值
        /// </summary>
        /// <param name="cookieStr">字符串值</param>
        /// <param name="field">域值</param>
        /// <returns>返回值</returns>
        private string GetUserSearCookieFieldVal(string cookieStr, string field)
        {
            if (cookieStr != "" && !String.IsNullOrEmpty(cookieStr))
            {
                if (field != "" && !String.IsNullOrEmpty(field))
                {
                    String[] tempCookStrArry = cookieStr.Split('&');
                    foreach (String tempCookStr in tempCookStrArry)
                    {
                        String[] tcsaField = tempCookStr.Split('=');
                        if (tcsaField.Length > 1)
                        {
                            if (tcsaField[0].ToLower() == field.ToLower())
                            {
                                return tcsaField[1];
                            }
                        }
                        else
                        {
                            return "";
                        }
                    }
                }
            }
            return "";
        }
    }
}
